﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;

namespace QLHPHP
{
    public partial class LapPhieuThuControl : UserControl
    {
        public LapPhieuThuControl()
        {
            InitializeComponent();
        }

        private AppCommands _Commands;

        /// <summary>
        /// Gets or sets the commands associated with the control.
        /// </summary>
        public AppCommands Commands
        {
            get { return _Commands; }
            set
            {
                if (value != _Commands)
                {
                    AppCommands oldValue = _Commands;
                    _Commands = value;
                    OnCommandsChanged(oldValue, value);
                }
            }
        }
        /// <summary>
        /// Called when Commands property has changed.
        /// </summary>
        /// <param name="oldValue">Old property value</param>
        /// <param name="newValue">New property value</param>
        protected virtual void OnCommandsChanged(AppCommands oldValue, AppCommands newValue)
        {
            if (newValue != null)
            {
                btnDong.Command = newValue.LapPhieuThuCommands.Cancel;
            }
            else
            {
                btnDong.Command = null;
            }
        }

        private void LapPhieuThuControl_Load(object sender, EventArgs e)
        {
            lblMS.Text = BienToanCuc.bientoancuc1;
            lblSoPhieuDK.Text = BienToanCuc.Sophieutoancuc;

            int day = DateTime.Now.Day;
            int month = DateTime.Now.Month;
            int year = DateTime.Now.Year;
            string ngaythu = day + "/" + month + "/" + year;
            lblNgayThu.Text = ngaythu;

            int mssv = int.Parse(lblMS.Text);
            lblHoTen.DataBindings.Add("Text", SinhVien.LayTenSVTuMSSV(mssv), "Ten");

            int spdk = int.Parse(lblSoPhieuDK.Text);
            lblSoTienPhaiDong.DataBindings.Add("Text", HocPhi.LayThongHPTheoSPDK(spdk), "SoTienPhaiDong");
            lblSoTenConLai.DataBindings.Add("Text", HocPhi.LayThongHPTheoSPDK(spdk), "SoTienConLai");

        }

        private void btnLapPhieu_Click(object sender, EventArgs e)
        {
            int stpd = int.Parse(lblSoTienPhaiDong.Text);
            int std = int.Parse(txtSoTienDaDong.Text);
            if (txtSoTienDaDong.Text == "")
            {
                ToastNotification.Show(this, "Số tiền nộp là bao nhiêu?");
            }
            else if (std>stpd)
            {
                ToastNotification.Show(this, "Số tiền đóng không thể lớn hơn tiền học phí");
            }
            else if (std <= 0)
            {
                ToastNotification.Show(this, "Số tiền nhập không hợp lệ");
            }
            else
            {
                try
                {
                    int SPDK = int.Parse(lblSoPhieuDK.Text);
                    int SoTienPhaiDong = int.Parse(lblSoTienPhaiDong.Text);
                    int SoTienDaDong = int.Parse(txtSoTienDaDong.Text);
                    DateTime ngaythu = DateTime.Parse(lblNgayThu.Text);

                    MonHoc.ThemHocPhiVaoPhieuDK(SPDK, SoTienDaDong);
                    HocPhi.CapNhapThongTinHocPhi();

                    HocPhi.TaoPhieuThu(SPDK, SoTienDaDong, ngaythu);
                    ToastNotification.Show(this, "Đã lập phiếu!");

                }
                catch
                {

                }
            }
        }

        private void txtSoTienDaDong_TextChanged(object sender, EventArgs e)
        {
            //string text = txtSoTienDaDong.Text;
            
            //text = text.Replace(",", "");
            ////text = String.Format("{0:0,0}", text);
            //int temp = Int32.Parse(text);
            ////txtSoTienDaDong.Text = "";
            //txtSoTienDaDong.Text = String.Format("{0:0,0}", temp);
        }
    }
}
